package com.ly.common.core.tool.mybatisPlus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ly.common.core.utils.BaseUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MP属性自动填充
 *
 * @author luoyun
 * @Date: 2025/5/17 15:07
 * @Note:
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    public String getUsername() {
        return BaseUtils.getUsername();
    }

    // 插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "sysCreateDate", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "sysUpdateDate", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "sysCreatorId", String.class, this.getUsername());
        this.strictInsertFill(metaObject, "sysUpdatorId", String.class, this.getUsername());
        //逻辑删除
        // this.setFieldValByName("delFlag", 0, metaObject);
    }

    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "sysUpdateDate", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "sysUpdatorId", String.class, this.getUsername());
    }
}
